package common.main.google;

import common.system.FileOperation;

import java.io.*;

public class Config {


    public static int searchTopKeyNum=2000;   // top-k
    public static String httpProxy=null;   //
    public static String mysqlHost="localhost:3306";  //
    public static String mysqlDatabaseName="jkw";   //
    public static String mysqlUserName="root";           //
    public static String mysqlPassword="root";     //
    public static String entranceUrl="https://scholar.google.com.hk/scholar?hl=zh-CN&as_sdt=0%2C5&q=<keyword>";
    public static String junkewei_webServer = "http://localhost:8080/scholar";
    public static String proxyHost = "192.168.3.92";
    public static int proxyPort = 1080;
    public static String proxyName = null;
    public static String proxyPwd = null;


//    searchTopKeyNum=30
//    mysqlHost=192.168.3.78:3306
//    mysqlDatabaseName=junkewei
//    mysqlUserName=pxqb
//    mysqlPassword=1!p@ssword



    public static void init(){
        //String content = FileOperation.read("config/config.txt");
        String path = ScholarPaperListStartMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String content = FileOperation.read(path.replace("BaseTest.jar","") + "config/config.txt");
        String []lines = content.split("\n");

        for(String line:lines){
            if(line.length()>3&&!line.startsWith("#")){
                line=line.trim();
                if(line.startsWith("mysqlHost")){
                    mysqlHost=line.split("=")[1].trim();
                }else if(line.startsWith("mysqlDatabaseName")){
                    mysqlDatabaseName=line.split("=")[1].trim();
                }else if(line.startsWith("mysqlUserName")){
                    mysqlUserName=line.split("=")[1].trim();
                }else if(line.startsWith("mysqlPassword")){
                    mysqlPassword=line.split("=")[1].trim();
                }else if(line.startsWith("searchTopKeyNum")){
                    searchTopKeyNum=Integer.parseInt(line.split("=")[1].trim());
                }else if(line.startsWith("entranceUrl")){
                    entranceUrl = line.split("entranceUrl=")[1].trim();
                }else if(line.startsWith("junkewei_webServer")){
                    junkewei_webServer = line.split("junkewei_webServer=")[1].trim();
                }else if(line.startsWith("proxyHost")){
                    proxyHost = line.split("proxyHost=")[1].trim();
                }else if(line.startsWith("proxyPort")){
                    proxyPort = Integer.valueOf(line.split("proxyPort=")[1].trim());
                }else if(line.startsWith("proxyName")){
                    proxyName = line.split("proxyName=")[1].trim();
                    if("null".equals(proxyName)){
                        proxyName = null;
                    }
                }else if(line.startsWith("proxyPwd")){
                    proxyPwd = line.split("proxyPwd=")[1].trim();
                    if("null".equals(proxyPwd)){
                        proxyPwd = null;
                    }
                }
            }
        }

    }

    //记录pid
    public static void addPID(String pid){
       try {
           String path = ScholarPaperListStartMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
           //File file = new File("config/PID.txt");
           File file = new File(path.replace("BaseTest.jar","") + "config/PID.txt");
           BufferedWriter bw = new BufferedWriter(new FileWriter(file));
           bw.write(pid);
           bw.flush();
           bw.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }
}
